package com.bnz.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;
import com.bnz.manager.dao.ItemCatDao;
import com.bnz.manager.entity.ItemCatEntity;
import com.bnz.manager.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("itemCatService")
public class ItemCatServiceImpl extends ServiceImpl<ItemCatDao, ItemCatEntity> implements ItemCatService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ItemCatEntity> page = this.page(
                new Query<ItemCatEntity>().getPage(params),
                new QueryWrapper<ItemCatEntity>()
        );

        return new PageUtils(page);
    }
/**
 * @author: zhanghao
 * @date: 2023/3/7 16:04
 * @param: []
 * @return: java.util.List<com.bnz.manager.entity.ItemCatEntity>
 * @功能: 查询分类并且以分类名为key,以模板id为值放到redis中
 **/
    @Override
    public List<ItemCatEntity> findAll() {
        //1.1 查出所有分类
        List<ItemCatEntity> list = this.list();
        //1.2 遍历分类,以分类名为key,以模板id为值
        for (ItemCatEntity itemCatEntity : list) {
            String name = itemCatEntity.getName();
            String typeId = itemCatEntity.getTypeId();
            BoundHashOperations<String, Object, Object> itemCatList = redisTemplate.boundHashOps("itemCatList");
            itemCatList.put(name,typeId);
        }
        return list;
    }

}